package com.veepeak.server.api.app;

import com.alibaba.fastjson.JSONObject;
import com.veepeak.server.common.JSONResult;
import com.veepeak.server.domain.*;
import com.veepeak.server.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/user")
@Api(description = "用户接口")
public class UserAppApi {
    @Autowired
    private UserService userService;
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private ObdService obdService;
    @Autowired
    private UserVehicleService userVehicleService;
    @Autowired
    private UserObdService userObdService;

    @ApiOperation(value = "用户修改密码", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱地址", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", paramType = "param", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "newPassword", value = "新密码", paramType = "param", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/edit-password")
    public JSONObject editPassword(String email, String oldPassword,String newPassword){
        try {
            User user = userService.editPassword(email,oldPassword,newPassword);
            return JSONResult.toSuccessJson(user,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson(e.getMessage());
        }
    }

    @ApiOperation(value = "获取用户信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "path", required = true, dataTypeClass = String.class)
    })
    @GetMapping(value = "/{userId}")
    public JSONObject get(@PathVariable String userId){
        return JSONResult.toSuccessJson(userService.get(userId));
    }

    @ApiOperation(value = "修改用户名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "nickname", value = "用户昵称", paramType = "body", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/edit-nickname")
    public JSONObject editNickname(String userId, String nickname){
        try {
            User user = userService.editNickname(userId,nickname);
            return JSONResult.toSuccessJson("success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

    @ApiOperation(value = "用户添加车辆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "车辆名称", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "year", value = "车辆年代", paramType = "body", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "vehicleModelId", value = "型号", paramType = "body", required = false, dataTypeClass = String.class)
    })
    @PostMapping(value = "/add-vehicle")
    public JSONObject setVehicle(String userId,String name,String year, String vehicleModelId){
        try {
            UserVehicle userVehicle = userService.setVehicle(userId,name,year,vehicleModelId);
            return JSONResult.toSuccessJson(userVehicle.getVehicle(),"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

    @ApiOperation(value = "用户修改车辆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vehicleId", value = "车辆ID", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "name", value = "车辆名称", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "year", value = "车辆年代", paramType = "body", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "vehicleModelId", value = "型号", paramType = "body", required = false, dataTypeClass = String.class)
    })
    @PostMapping(value = "/edit-vehicle")
    public JSONObject editVehicle(String vehicleId,String name,String year, String vehicleModelId){
        try {
            Vehicle vehicle = userService.editVehicle(vehicleId,name,year,vehicleModelId);
            return JSONResult.toSuccessJson(vehicle,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

    @ApiOperation(value = "用户删除车辆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "vehicleId", value = "车辆ID", paramType = "body", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/remove-vehicle")
    public JSONObject removeVehicle(String userId,String vehicleId){
        try {
            userService.removeVehicle(userId,vehicleId);
            return JSONResult.toSuccessJson(null,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

//    @ApiOperation(value = "用户添加OBD")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "body", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(name = "serialsNumber", value = "产品序列号", paramType = "body", required = true, dataTypeClass = String.class),
//            @ApiImplicitParam(name = "productModel", value = "产品型号", paramType = "body", required = true, dataTypeClass = String.class)
//    })
//    @PostMapping(value = "/add-obd")
//    public JSONObject setObd(String userId,String serialsNumber, String productModel){
//        try {
//            UserObd userObd = userService.setObd(userId,serialsNumber,productModel);
//            return JSONResult.toSuccessJson(userObd.getObd(),"设置成功");
//        } catch (Exception e) {
//            return JSONResult.toFailureJson("设置失败:"+e.getMessage());
//        }
//    }

    @ApiOperation(value = "用户添加OBD")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "bluetoothName", value = "蓝牙名称", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "bluetoothAddress", value = "蓝牙地址", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "obdModel", value = "产品型号", paramType = "body", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/add-obd")
    public JSONObject setObd(String userId,String bluetoothName, String bluetoothAddress, String obdModel){
        try {
            UserObd userObd = userService.setObd(userId,obdModel,bluetoothAddress,bluetoothName);
            return JSONResult.toSuccessJson(userObd,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

    @ApiOperation(value = "用户修改OBD")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obdId", value = "车辆ID", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "serialsNumber", value = "产品序列号", paramType = "body", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "productModel", value = "产品型号", paramType = "body", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/edit-obd")
    public JSONObject editObd(String obdId,String serialsNumber,String productModel){
        try {
            Obd obd = userService.editObd(obdId, serialsNumber, productModel);
            return JSONResult.toSuccessJson(obd,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

    @ApiOperation(value = "用户删除OBD")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userObdId", value = "用户ID", paramType = "body", required = true, dataTypeClass = String.class)
    })
    @PostMapping(value = "/remove-obd")
    public JSONObject removeObd(String userObdId){
        try {
            userService.removeObd(userObdId);
            return JSONResult.toSuccessJson(null,"success");
        } catch (Exception e) {
            return JSONResult.toFailureJson("fail:"+e.getMessage());
        }
    }

    @ApiOperation(value = "获取用户车辆列表")
    @GetMapping(value = "/{userId}/vehicle")
    public JSONObject getVehicleList(@PathVariable String userId){
        try {
            List<Vehicle> vehicleList = userVehicleService.findByUserId(userId);
            return JSONResult.toSuccessJson("vehicles",vehicleList,"");
        } catch (Exception e) {
            return JSONResult.toFailureJson(e.getMessage());
        }
    }

    @ApiOperation(value = "获取用户obd列表")
    @GetMapping(value = "/{userId}/obd")
    public JSONObject getObdList(@PathVariable String userId){
        try {
            List<UserObd> obdList = userObdService.findByUserId(userId);
            return JSONResult.toSuccessJson("obds",obdList,"");
        } catch (Exception e) {
            return JSONResult.toFailureJson(e.getMessage());
        }
    }
}
